﻿namespace SubhadraSolutions.Sharp.Utils.TestApplication
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.ServiceProcess;
    using System.Windows.Forms;

    using SubhadraSolutions.Sharp.Utils.Contracts;
    using SubhadraSolutions.Sharp.Utils.ServiceProcess;

    public static class ServiceProcessLauncher
    {
        public static void Launch()
        {
            List<IControllable> controllables = BuildDummyComponents();
            bool runAsService = false;
            string runAsServiceString = ConfigurationManager.AppSettings["runAsService"];
            if (runAsServiceString != null)
            {
                bool.TryParse(runAsServiceString, out runAsService);
            }

            if (runAsService)
            {
                var ServicesToRun = new ServiceBase[] { new ControllableAsServiceBaseAdapter { Controllables = controllables } };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1(controllables));
            }
        }

        /// <summary>
        /// The build dummy components.
        /// </summary>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        private static List<IControllable> BuildDummyComponents()
        {
            var random = new Random();
            var controllables = new List<IControllable>();
            int limit = random.Next(5) + 1;
            for (int i = 0; i < limit; i++)
            {
                var d = new DummyControllable { Name = "Dummy" + i };
                controllables.Add(d);
                bool b1 = random.Next() % 2 == 0;
                if (b1)
                {
                    var composite = new CompositeControllableComponent { Name = "Composite" + i };
                    controllables.Add(composite);
                    int limit1 = random.Next(5) + 1;
                    for (int j = 0; j < limit1; j++)
                    {
                        d = new DummyControllable { Name = "Dummy" + j };
                        composite.Controllables.Add(d);
                        bool b2 = random.Next() % 2 == 0;
                        if (b2)
                        {
                            var composite1 = new CompositeControllableComponent { Name = "Composite" + j };
                            composite.Controllables.Add(composite1);
                            int limit2 = random.Next(2) + 1;
                            for (int k = 0; k < limit2; k++)
                            {
                                d = new DummyControllable { Name = "Dummy" + k };
                                composite1.Controllables.Add(d);
                            }
                        }
                    }
                }
            }

            return controllables;
        }
    }
}
